window.KKBus = {};

(function(KKBus){  
    
    function Application(){ 
        var self = this;

        self.subpage = 'empty-template';
        self.request = 0;
        
        self.settings = {
            DEFAULT_HASH: 'home',
            PREPEND_HASH: '',
            BASE_URL: hasher.getBaseURL(),
            DEBUG: false
        };
        
        self.loadTemplate = function() {
            $('#template').html('');
            $('#template').html('<div data-bind="template: { name: \'main-template\', data: $root, afterRender: jQueryUI.init }"></div>');
            ko.applyBindings(self.Knockout);
        };
        
        self.parseHash = function(newHash, oldHash){
            crossroads.parse(newHash);
        };
        
        self.Knockout = null;
        
        self.run = function(isAuthenticated){
            // Knockout
            self.Knockout = {
                jQueryUI: new KKBus.jQueryUI(),
                AuthenticationController: new KKBus.AuthenticationController(),
                NewsController: new KKBus.NewsController(),
                GiftsController: new KKBus.GiftsController(),
                BusStopController: new KKBus.BusStopController(),
                TicketPriceController: new KKBus.TicketPriceController(),
                HistoryLogController: new KKBus.HistoryLogController(),
                UserController: new KKBus.UserController(),
                AdminController: new KKBus.AdminController(),
                BusController: new KKBus.BusController(),
                ScheduleController: new KKBus.ScheduleController(),
                DefectController: new KKBus.DefectController(),
                RefuelingController: new KKBus.RefuelingController(),
                ExchangeController: new KKBus.ExchangeController(),
                ReservationController: new KKBus.ReservationController()
            };
            
            // Inicjalizacja danych użytkownika
            if (isAuthenticated == true) {
                app.Knockout.AuthenticationController.loadAuthenticatedUser();
            } else {
                app.Knockout.AuthenticationController.loadAnonymousUser();
            }
            
            // Hasher & Crossroads  
            hasher.prependHash = self.settings.PREPEND_HASH;
            /*
            if (!hasher.getHash()) {
                hasher.setHash(self.settings.DEFAULT_HASH);
            }
            */
            crossroads.addRoute('', function() { 
                self.subpage = 'home-template';                
                if (self.request == 0) self.loadTemplate();
            });
            crossroads.addRoute('home', function() {
                self.subpage = 'home-template';                
                if (self.request == 0) self.loadTemplate();              
            });
            crossroads.addRoute('contact', function() {  
                self.subpage = 'contact-template';                
                if (self.request == 0) self.loadTemplate();
            });
            crossroads.addRoute('registration', function() {  
                self.subpage = 'registration-template';
                if (self.request == 0) self.loadTemplate();
            });
            
            // TODO Poprawki
            crossroads.addRoute('overviewcourses', function() {  
                if (self.settings.DEBUG == true) { console.log("overviewcourses subpage called"); }
                self.subpage = 'overviewcourses-template';
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
            
            crossroads.addRoute('searchcourses', function() {  
                if (self.settings.DEBUG == true) { console.log("searchcourses subpage called"); }
                self.subpage = 'searchcourses-template';
                
                $('#ajax-loader').show();
                self.Knockout.BusStopController.getCourses();
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
               crossroads.addRoute('ticketprice', function() {  
                if (self.settings.DEBUG == true) { console.log("ticketprice subpage called"); }
                self.subpage = 'ticketprice-template';
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
             
            
            crossroads.addRoute('editprofile', function() {  
                if (self.settings.DEBUG == true) { console.log("editprofile subpage called"); }
                self.subpage = 'editprofile-template';
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
            
            crossroads.addRoute('/gifts/:page:', function(page) {  
                if (typeof page === "undefined") {
                    page = 0;
                }
                if (self.settings.DEBUG == true) { console.log("gifts subpage called - page: " + page); }
                
                self.subpage = 'gifts-template';                
                $('#ajax-loader').show();
                self.Knockout.GiftsController.getGifts(page);
            });
            
            crossroads.addRoute('/news/:page:', function(page) {
                if (typeof page === "undefined") {
                    page = 0;
                }
                if (self.settings.DEBUG == true) { console.log("news subpage called - page: " + page); }
                
                self.subpage = 'news-template';                
                $('#ajax-loader').show();
                self.Knockout.NewsController.getNews(page);
            });
            
            crossroads.addRoute('/user/activate/{hash}', function(hash) {
                if (self.settings.DEBUG == true) { console.log("activate user subpage called. Hash: " + hash); }
                
                self.subpage = 'user-activate-template';
                $('#ajax-loader').show();
                
                app.Knockout.AuthenticationController.activateUser(hash);
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });            
            
            
             crossroads.addRoute('booking', function() {  
                if (self.settings.DEBUG == true) { console.log("booking subpage called"); }
                self.subpage = 'booking-template';
                
                $('#ajax-loader').show();
                self.Knockout.ReservationController.getHistoryBook();
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
             crossroads.addRoute('history', function() {  
                if (self.settings.DEBUG == true) { console.log("history subpage called"); }
                self.subpage = 'history-template';
                
                $('#ajax-loader').show();
                self.Knockout.ReservationController.getHistory();
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
            crossroads.addRoute('historylog', function() {  
                if (self.settings.DEBUG == true) { console.log("historylog subpage called"); }
                self.subpage = 'historylog-template';
                
                $('#ajax-loader').show();
                self.Knockout.HistoryLogController.getHistoryLog();
                
              if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
              crossroads.addRoute('employelist', function() {  
                if (self.settings.DEBUG == true) { console.log("employelist subpage called"); }
                self.subpage = 'employelist-template';
                
                $('#ajax-loader').show();
                self.Knockout.AdminController.getWorker();
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
             crossroads.addRoute('clientlist', function() {  
                if (self.settings.DEBUG == true) { console.log("clientlist subpage called"); }
                self.subpage = 'clientlist-template';
                
                $('#ajax-loader').show();
                self.Knockout.UserController.getUser();
               
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
              crossroads.addRoute('points', function() {  
                if (self.settings.DEBUG == true) { console.log("points subpage called"); }
                self.subpage = 'points-template';
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
             crossroads.addRoute('managingroutes', function() {  
                if (self.settings.DEBUG == true) { console.log("managingroutes subpage called"); }
                self.subpage = 'managingroutes-template';
                
                
                  $('#ajax-loader').show();
                self.Knockout.BusStopController.getCourses();

                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
             crossroads.addRoute('schedule', function() {  
                if (self.settings.DEBUG == true) { console.log("schedule subpage called"); }
                self.subpage = 'schedule-template';
                
                 $('#ajax-loader').show();
                self.Knockout.ScheduleController.getSchedule();
                
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            
            crossroads.addRoute('list', function() {  
                if (self.settings.DEBUG == true) { console.log("list subpage called"); }
                self.subpage = 'list-template';
                
                $('#ajax-loader').show();
                self.Knockout.BusController.getBus();
		                
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
            crossroads.addRoute('statistics', function() {
                if (self.settings.DEBUG == true) { console.log("statistics subpage called"); }
                self.subpage = 'statistics-template';

                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });
			crossroads.addRoute('popup', function() {  
                if (self.settings.DEBUG == true) { console.log("popup subpage called"); }
                self.subpage = 'popup-template';
                
                if (self.request == 0) {
                    if (self.settings.DEBUG == true) { console.log("Load template from: " + Application.name); }
                    self.loadTemplate();
                }
            });          
            hasher.initialized.add(self.parseHash); //parse initial hash
            hasher.changed.add(self.parseHash); //parse hash changes
            hasher.init(); //start listening for hash changes
        };        
    }
    
    KKBus.Application = Application;
    
}(window.KKBus));